package cn.lanqiao.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.lanqiao.entity.Pages;
import cn.lanqiao.entity.Teacher;

@CrossOrigin
@RequestMapping("/api/teacher")
@RestController
public class TeacherController {

	@Autowired
	private cn.lanqiao.service.TeacherService teacherService;
	
	
	//分页
	@GetMapping(value="/teacherpage")
	public Pages<Teacher> pager(
			@RequestParam(value="pageindex") int pageindex,
			@RequestParam(value="pagesize") int pagesize,
			@RequestParam(value="searchkey",required=false,defaultValue = "") String searchkey){
		return teacherService.teacherPage(pageindex, pagesize, searchkey);
	}
	
	//获取单条数据
	@ GetMapping("/get/{teacherId}")
	public Map<String,Object> get(@PathVariable (value="teacherId") Integer teacherId){
		Map<String,Object>  map = new HashMap<String,Object>();
		Teacher teacher = teacherService.getTeacherById(teacherId);
		if(teacher!=null) {
			map.put("data",teacher);
			map.put("status", 200);
			map.put("statusText","获取数据成功 ");
		}else {
			map.put("data",null);
			map.put("status", 0);
			map.put("statusText","获取数据失败 ");
		}
		return map;
	}
	
	//更新
	@PutMapping("/update")
	public Map<String , Object > update( @RequestBody  Teacher teacher ){
		int result = teacherService.updateTeacher(teacher);
		Map<String,Object>  map = new HashMap<String,Object>();
		if( result >0 ) {
			map.put("status", 200);
			map.put("statusText", "更新成功");
		} else {
			map.put("status", 0);
			map.put("statusText", "更新失败");
		}
		return map;
	}
	
	//删除
	@DeleteMapping("/delete/{teacherId}")
	public Map<String,Object> delete(@PathVariable (value="teacherId") Integer teacherId){
		Map<String,Object>  map = new HashMap<String,Object>();
		int result = teacherService.deleteTeacher(teacherId);
		if(result>0) {
			map.put("status", 200);
			map.put("statusText", "删除成功");
		}else{
			map.put("status", 0);
			map.put("statusText", "删除失败");
		}
		return map;
	}
	
}
